/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.appleframework.web.results;

import java.util.Map;
import org.appleframework.Apple;
import org.appleframework.exceptions.UnexpectedException;
import org.appleframework.templates.Template;
import org.appleframework.web.HttpRequest;
import org.appleframework.web.HttpResponse;

/**
 *
 * @author zhujg
 */
public class RenderTemplate extends Result {

  private Template template;
  Map<String, Object> args;

  public RenderTemplate(Template template){
     this.template = template;
  }
  public RenderTemplate(Template template, Map<String, Object> args) {
    this.template = template;
    this.args = args;
  }

  @Override
  public void apply(HttpRequest request, HttpResponse response) {
    String content = template.source;
    setContentTypeIfNotSet(response, "text/plain");
    try {
      response.out.write(content.getBytes(Apple.encode));
    } catch (Exception e) {
      throw new UnexpectedException(e);
    }
  }
}
